package com.guodefu.demo.config;

import com.redis.testcontainers.RedisContainer;
import org.springframework.boot.devtools.restart.RestartScope;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.MongoDBContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

@TestConfiguration(proxyBeanMethods = false)
public class LocalDevTestcontainersConfig {
    @Bean
    @RestartScope
    @ServiceConnection(name = "postgres")
    public PostgreSQLContainer<?> postgresqlDBContainer() {
        return new MyPostgreSQLContainer("postgres:16-alpine")
                .withExposedPorts(5432)
                .withFixedExposedPort(15432, 5432)
                ;
    }

    @Bean
    @RestartScope
    @ServiceConnection(name = "mongo")
    public MongoDBContainer mongoDBContainer() {
        return new MyMongoDBContainer("mongo:6.0")
                .withFixedExposedPort(27017, 27017)
                .withExposedPorts(27017)
                ;
    }

    /**
     * '@ServiceConnection' name 属性提供了一个唯一标识符，用于标识和区分多个不同的服务连接配置。
     * 例如，如果你有多个 Testcontainers 容器（如 Redis、MySQL 等），name 属性可以确保每个容器的连接配置正确匹配。
     */
    @Bean
    @RestartScope
    @ServiceConnection(name = "redis")
    public RedisContainer redisContainer() {
        return new MyRedisContainer("redis:7.2-alpine")
                .withFixedExposedPort(16379, 6379)
                .withExposedPorts(6379)
                ;
    }

    @Bean
    @ServiceConnection
    public KafkaContainer kafkaContainer() {
        return new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:6.2.1"))
//                .withExposedPorts(7088, 7089)
                ;
    }

}
